package fr.afcepf.al22.qualite.data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import javax.sql.DataSource;

import fr.afcepf.al22.qualite.datasource.Al22DataSource;
import fr.afcepf.al22.qualite.entitee.Personne;
/**
 * .
 * @author Stagiaire
 *
 */
public class DaoPersonneImpl implements IDaoPersonne {
    /**
     * .
     */
    private DataSource ds  = new Al22DataSource();
    /**
     * .
     */
    private String reqAjout =
            "INSERT INTO personne (nom,mail,mdp,naissance)"
                    + "VALUES (?,?,?,?)";
    /**
     * .
     */
    private final int indiceNomAjout = 1;
    /**
     * .
     */
    private final int indiceMailAjout = 2;
    /**
     * .
     */
    private final int indiceMdpAjout = 3;
    /**
     * .
     */
    private final int indiceNaissanceAjout = 4;
    /**
     * .
     */
    private int idGenerated = 1;
    @Override
    public Personne ajouter(Personne paramPers) {
        Connection cnx = null;
        try {
            cnx = ds.getConnection();
            PreparedStatement pstmt =
                    cnx.prepareStatement(reqAjout,
                            Statement.RETURN_GENERATED_KEYS);
            pstmt.setString(indiceMdpAjout, paramPers.getMdp());
            pstmt.setString(indiceMailAjout, paramPers.getMail());
            pstmt.setDate(indiceNaissanceAjout, new java.sql.Date(paramPers
                    .getNaissance().getTime()));
            pstmt.setString(indiceNomAjout, paramPers.getNom());
            int nbLigne = pstmt.executeUpdate();
            ResultSet rskey = pstmt.getGeneratedKeys();
            if (nbLigne == 1 && rskey.next()) {
                paramPers.setId(rskey.getInt(idGenerated));
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                cnx.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return paramPers;
    }

    @Override
    public List<Personne> rechercher(String paramNom) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Personne verifMail(String paramMail) {
        // TODO Auto-generated method stub
        return null;
    }

}
